﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Social_Celebrities
{
    public class DateToString : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                DateTime dt = (DateTime)value;
                TimeSpan timesince = DateTime.Now - dt;
                if (timesince.Days > 365)
                {
                    int years = (timesince.Days / 365);
                    if (timesince.Days % 365 != 0)
                        years += 1;
                    return String.Format("about {0} {1} ago", years, years == 1 ? "year" : "years");
                }
                else if (timesince.Days > 30)
                {
                    int months = (timesince.Days / 30);
                    if (timesince.Days % 31 != 0)
                        months += 1;
                    return String.Format("about {0} {1} ago", months, months == 1 ? "month" : "months");
                }
                else if (timesince.Days > 0)
                    return String.Format("about {0} {1} ago", timesince.Days, timesince.Days == 1 ? "day" : "days");
                else if (timesince.Hours > 0)
                    return String.Format("about {0} {1} ago", timesince.Hours, timesince.Hours == 1 ? "hour" : "hours");
                else if (timesince.Minutes > 0)
                    return String.Format("about {0} {1} ago", timesince.Minutes, timesince.Minutes == 1 ? "minute" : "minutes");
                else if (timesince.Seconds > 5)
                    return String.Format("about {0} seconds ago", timesince.Seconds);
                else
                    return "just now";
            }
            catch (Exception ex)
            {
                string Mesg = ex.Message;
                return null;
            }
        }       

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
